<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<head>
    <title>实时聊天</title>
</head>
<body>
<div id="chat">
    <div id="messages"></div>
    <input id="messageInput" placeholder="输入消息..." type="text">
    <button onclick="sendMessage()">发送</button>
</div>
<script>
    let ws;

    function connect() {
        //你的websocket
        ws = new WebSocket('ws://localhost:32001/daimouren');

        ws.onmessage = function (event) {
            let message = event.data;
            document.getElementById('messages').innerHTML += '<div>' + message + '</div>';
        };

        ws.onerror = function (event) {
            console.error("WebSocket error observed:", event);
        };
    }
    function sendMessage() {
        let message = document.getElementById('messageInput').value;
        ws.send(message);
        document.getElementById('messageInput').value = '';
    }

    window.onload = connect;
</script>
</body>
</html>
